﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUUserToRelation
    {
        /// <summary>
        /// Get All the UserToRelation of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<UserToRelation> GetAllUserToRelation(bool searchInDeleted)
        {
            List<UserToRelation> UserToRelations = new List<UserToRelation>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from UserToRelation in db.UserToRelation
                              where UserToRelation.utr_isdeleted == searchInDeleted
                              select UserToRelation;
                foreach (UserToRelation tmp in results)
                    UserToRelations.Add(tmp);
            }

            return UserToRelations;
        }

        /// <summary>
        /// Get the UserToRelation by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UserToRelation GetUserToRelationByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var utr = from UserToRelation in db.UserToRelation
                              where UserToRelation.utr_id == id
                              select UserToRelation;
                    foreach (UserToRelation tmp in utr)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// utr a UserToRelation into the database.
        /// </summary>
        /// <param name="utr"></param>
        /// <returns></returns>
        public static UserToRelation UserToRelationrea(UserToRelation utr)
        {
            if (utr != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    UserToRelation nutr = db.UserToRelation.Add(utr);
                    db.SaveChanges();
                    return nutr;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific UserToRelation of the database.
        /// </summary>
        /// <param name="utr"></param>
        /// <returns></returns>
        public static bool RemoveUserToRelation(UserToRelation utr)
        {
            if (utr != null)
            {
                utr.utr_isdeleted = true;
                if (UpdateUserToRelation(utr) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the UserToRelation into the database.
        /// </summary>
        /// <param name="utr"></param>
        /// <returns></returns>
        public static UserToRelation UpdateUserToRelation(UserToRelation utr)
        {
            if (utr != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from UserToRelation in db.UserToRelation
                                  where UserToRelation.utr_id == utr.utr_id
                                  select UserToRelation;

                    foreach (var tmp in current)
                    {
                        tmp.usr_id1 = utr.usr_id1;
                        tmp.usr_id2 = utr.usr_id2;
                        tmp.rel_type = utr.rel_type;
                        tmp.utr_approved = utr.utr_approved;
                        tmp.cir_id = utr.cir_id;
                        tmp.utr_isdeleted = utr.utr_isdeleted;

                        db.SaveChanges();
                    }

                    utr = current as UserToRelation;
                }
            }

            return utr;
        }
    }
}
